package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加到购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        ShoppingCart shopCarttt = shoppingCartService.add(shoppingCart);
        return R.success(shopCarttt);
    }

    /**
     * 查看购物车
     */
    @GetMapping("/list")
    public R list() {
        List<ShoppingCart> list = shoppingCartService.list();
        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R clean() {
        try {
            shoppingCartService.clean();
            return R.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("删除失败");
        }

    }

    @PostMapping("sub")
    public R delete(@RequestBody ShoppingCart shoppingCart) {
        ShoppingCart shoppingCart1 = shoppingCartService.sub(shoppingCart);
        return R.success(shoppingCart1);
     /*   try {

        } catch (Exception e) {
            e.printStackTrace();
            return R.error("清除购物车失败");
        }*/
    }
}
